<?php
class BBSAction extends Action{
	private $fs; // Forum model
	private $pv; // Post view
	private $pm; // Post model
	private $pr; // Post reply model
	private $prv; // Post reply view

	public function __construct()
	{
		parent::__construct();
		$this->fs = new ForumModel();
		$this->pv = new PostView();
		$this->pm = new PostModel();
		$this->pr = new PostReplyModel();
		$this->prv = new PostReplyView();

		if($this->session->getAttr("uid") == null){
			$this->forward("Index");
		}
	}
	
	protected function init()
	{

	}
	public function display()
	{
		
	}
	public function doAddPr()
	{
		$signatures = array(
			'pr_post_id' => array('name' => '帖子ID', 'required' => true, 'type' => 'int'),
			'pr_text' => array('name' => '回复内容', 'required' => true, 'type' => 'string')
			);

		$this->checkVars($this->post, $signatures);

		$this->pr->create();
		$this->pr->pr_author_id = $this->session->getAttr('uid');

		if($this->pr->add()){
			$msg = "添加回复成功";
			$work = new PostModel();
			$work->addCondition('pid', '=', $this->pr->pr_post_id, "");
			$work->findOne();
			$work->reply_number++;
			$work->addCondition('pid', '=', $this->pr->pr_post_id, "");
			$work->update();
		}else{
			$msg = "添加回复失败!";
		}
		$this->showMessage($msg);
	}
	public function showBBS()
	{
		$this->fs->addCondition('parent_id', '=', 0, 'order by id asc');
		$forums = $this->fs->find();
		$forum_id = $this->get['fid'] ? $this->get['fid'] : $forums[0]['id'];
		$forum_id = (int)$forum_id;

		$this->fs->addCondition('parent_id', '=', $forum_id, 'order by id asc');
		$sub_forums = $this->fs->find();

		$subforums = array();

		$subpost = new SubForumPostView();
		$subpost->setNumber(2);
		$subposts = $subpost->find();

		$num = count($sub_forums);
		$x = 0;
		$map = array();
		foreach($sub_forums as $forum)
		{
			$subforums[] = array(
				'forum' => $forum,
				'posts' => array()
				);
			$map[$forum['id']] = $x;
			$x++;
		}
		foreach($subposts as $post)
		{
			$index = $map[$post['forum_id']];
			$subforums[$index]['posts'][] = $post;
		}

		$this->smarty->assign('forums', $forums);
		$this->smarty->assign('subforums', $subforums);
		$this->smarty->display('bbs.tpl');
	}
	public function doView()
	{
		$this->showBBS();
	}
	public function showForum()
	{
		$fid = (int)$this->get['fid'];
		if($fid <= 0)
		{
			$this->showMessage("对不起，您要访问的论坛不存在！");
		}
		$this->pv->addCondition('forum_id', '=', $fid, '');
		$this->fs->addCondition('id', '=', $fid, '');

		$posts = $this->pv->find();
		$forum = $this->fs->findOne();

		$this->smarty->assign('posts', $posts);
		$this->smarty->assign('forum', $forum);
		$this->smarty->display('BBS-forum.tpl');
	}
	public function addPost()
	{
		$fid = (int)$this->get['fid'];
		if($fid <= 0)
		{
			$this->showMessage("对不起，您要访问的论坛不存在！");
		}
		$this->fs->addCondition('id', '=', $fid, '');
		$forum = $this->fs->findOne();
		$this->smarty->assign('forum', $forum);
		$this->smarty->display('BBS-addPost.tpl');
	}
	public function doAddPost()
	{
		$sigs = array(
			'forum_id' => array('required' => true, 'type' => 'int', 'name' => '论坛ID'),
			'title' => array('required' => true, 'type' => 'string', 'name' => '帖子标题'),
			'text' => array('required' => true, 'type' => 'string', 'name' => '帖子内容')
			);
		$this->checkVars($this->post, $sigs);
		$this->pm->create();
		$this->pm->author_id = $this->session->getAttr('uid');

		if($this->pm->add())
		{
			$msg = "发表帖子成功！";
		}else{
			$msg = "发表帖子失败！";
		}
		$this->showMessage($msg);
	}
	public function showPost()
	{
		$pid = (int) $this->get['pid'];

		if($pid <=0)
			$this->showMessage("您要访问的页面不存在！");
		$this->pv->addCondition('pid', '=', $pid, '');
		$post = $this->pv->findOne();

		$this->prv->addCondition('pr_post_id', '=', $pid, '');
		$replys = $this->prv->find();

		$this->smarty->assign('post', $post);
		$this->smarty->assign('replys', $replys);

		$this->smarty->display('BBS-showPost.tpl');
	}
	public function initForum()
	{
		$forums = array(
			'建筑学', '城市规划学', '环境艺术学', '景观学', '室内设计', 'Tea House'
			);
		/*
		foreach($forums as $forum)
		{
			$pid = $this->addForum($forum, 0);
			$this->addForum("我的设计", $pid);
			$this->addForum("考研", $pid);
			$this->addForum($forum . "论坛", $pid);
		}
		 */
	}
	public function addForum($name, $parent_id)
	{
		$forum = new ForumModel();

		$forum->name = $name;
		$forum->parent_id = $parent_id;

		$forum->add();
		return mysql_insert_id();
	}
};
?>

